<?php
// +----------------------------------------------------------------------
// | 后台用户管理
// +----------------------------------------------------------------------
namespace app\admin\controller\v1;
use app\admin\Adminbase;
use base\files\Poster;
use app\admin\model\SystemParamValue;

class Coupon extends Adminbase 
{
	
	/**
	* 获取可用优惠券
	* /admin/v1/coupon_issue/xmlLotteryGetCoupon
	* 需要登录 
	* @is_need_login true
	*/
	public function xmlLotteryGetCoupon($ids=0,$start_time='',$is_action_call=false){
		try {
			$result = model('Coupon')->getCoupon($ids,$start_time,true);
			if(!$result){
				return $this->ReError($result);
			}
			if($is_action_call){
				return $result;	
			}else{
				return $this->ReSucess($result);	
			}
		} catch (Exception $e) {
			throw new \think\Exception( $e );
		}
	}
	
	/**
	* 删除
	* /admin/v1/coupon/del
	* 需要登录 
	* @is_need_login true
	*/
	public function del($ids){
		if(!$ids){
			return $this ->  ReError("参数错误");
		}
		try {
			$result = model('Coupon')->setDel($ids,1);
			if(!$result){
				return $this->ReError($result);
			}
			return $this->ReSucess(true);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 设置启用与禁用
	* /admin/v1/coupon_issue/setEnable
	* 需要登录 
	* @is_need_login true
	*/
	public function setEnable($ids,$state){
		try {
			$result = model('Coupon')->setEnable($ids,$state);
			if(!$result){
				return $this->ReError($result);
			}
			return $this->ReSucess(true);	
		} catch (\Exception $e) {
			return $this->ReSucess($e->getMessage());	
		}
	}
	
	
	
	/**
	* 获取可用优惠券
	* /admin/v1/coupon_issue/getCoupon
	* 需要登录 
	* @is_need_login true
	*/
	public function getCoupon($ids=0,$start_time='',$is_action_call=false){
		try {
			$result = model('Coupon')->getCoupon($ids,$start_time);
			if(!$result){
				return $this->ReError($result);
			}
			if($is_action_call){
				return $result;	
			}else{
				return $this->ReSucess($result);	
			}
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 设置数量
	* /admin/v1/coupon_issue/setNum
	* 需要登录 
	* @is_need_login true
	*/
	public function setNum($id,$add_num=0,$reduce_num=0){
		try { 
			$model =  model('Coupon');
			$result =  $model->setTotalNum($id,$add_num,$reduce_num);
			if($result !== true){
				return $this->ReError( $model->errorInfo );	
			}else{
				return $this->ReSucess(true);	
			}
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 恢复数量
	* /admin/v1/coupon_issue/replyNum
	* 需要登录 
	* @is_need_login true
	*/
	public function replyNum($id,$num=0){
		try { 
			$model =  model('Coupon');
			$result =  $model->replyNum($id,$num);
			if($result !== true){
				return $this->ReError( $model->errorInfo );	
			}else{
				return $this->ReSucess(true);	
			}
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}

    /**
     * 设置作废
     * /admin/v1/coupon/setStop
     * 需要登录
     * @is_need_login true
     */
    public function setStop($ids,$state){
        try {
            $result = model('Coupon')->where('id',$ids)->update(['status'=>$state]);
            if(!$result){
                return $this->ReError($result);
            }
            return $this->ReSucess(true);
        } catch (\Exception $e) {
            return $this->ReSucess($e->getMessage());
        }
    }


    /**
     * 变更优惠券下的字段商品id
     * /admin/v1/coupon/setCouponGs
     * 需要登录
     * @is_need_login true
     */
    public function setCouponGs($coupon_id,$goodData){
        try {
            //拼接

                $goodData=json_decode($goodData,true);

		         $cost = array_unique(array_column($goodData, 'name'));
		         if(count($cost) != count($goodData)){
		             return $this->ReError("选择商品重复，请检查！");
		         }


                $gid_str='';
                foreach ($goodData as $k=>$v) {
                    $gid_str=$gid_str.$v['good_id'];
                    if($k!=count($goodData)-1)
                        $gid_str=$gid_str.',';
                }

                model('Coupon')->where('id',$coupon_id)->update(['goods_id'=>$gid_str]);
                model('Coupon')->where('id',$coupon_id)->update(['update_user'=>request() ->userInfo['user_name'],'update_time'=>date('Y-m-d H:i:s',time())]);


            return $this->ReSucess(true);

        } catch (\Exception $e) {
            return $this->ReSucess($e->getMessage());
        }
    }

    /**
     * 发放优惠券
     * /api/coupon/receive
     * 需要登录
     * @is_need_login true
     */
    public function receive($parent_id,$user_id,$usernum,$giftnum){

        if( !trim($parent_id) ){
            return $this->ReError('找不到优惠券id！');
        }

        //可使用的数量
        $couponmodel = model("Coupon","model");
        if($usernum){
        	for($i=0;$i<$usernum;$i++){
            	$result_user = $couponmodel->grant($parent_id,$user_id);
       		}
        }
        

        //可赠送的数量
		if($giftnum){
//	        for($i=0;$i<$giftnum;$i++){
				$result_gift = $couponmodel->grant($parent_id,$user_id,1,$giftnum);
//			}
        }

        if(!$result_user && !$result_gift){
            return $this->ReError( $couponmodel->errorInfo );
        }else{
            return $this->ReSucess('','发放成功！');
        }

    }




}

